//
//  FWAuthObject.m
//  FWShare
//
//  Created by 范文青 on 14-6-14.
//  Copyright (c) 2014年 fwq. All rights reserved.
//

#import "FWAuthObject.h"
#import <objc/runtime.h>
@implementation FWAuthObject
- (NSArray *)allProperties
{
    NSMutableArray  *propertyKeys = [NSMutableArray array];
    unsigned int outCount = 0;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (int i = 0; i < outCount; i++)
    {
        objc_property_t property = properties[i];
        NSString *key=[[NSString alloc] initWithCString:property_getName(property)
                                               encoding:NSUTF8StringEncoding];
        [propertyKeys addObject:key];
    }
    free(properties);
    return propertyKeys;
}

-(NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary  *params = [NSMutableDictionary dictionary];
    NSArray  *propertyList = [self allProperties];
    [propertyList enumerateObjectsUsingBlock:^(id key, NSUInteger idx, BOOL *stop){
        id value=[self valueForKey:key];
        if (value != nil)
        {
            id valueC = [self getValueForObject:value key:key];
            if (valueC == nil) {
                [params setObject:value forKey:key];
            }else{
                [params setObject:valueC forKey:key];
            }
        }
    }];
    return [NSDictionary dictionaryWithDictionary:params];
}

- (instancetype)initWithDictionary:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            @try {
                [self setValue:obj forKey:key];
            }
            @catch (NSException *exception) {
                NSLog(@"no such key : %@ for class : %@",key,[self class]);
            }

        }];
    }
    return self;
}
- (id)getValueForObject:(id)obj key:(NSString *)key{
    return nil;
}
@end
